package com.inspur.jxpcxt.activitys;

import java.lang.reflect.Field;
import java.util.Random;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.inspur.jxpcxt.LoginActivity;
import com.inspur.jxpcxt.constants.Constants;
import com.inspur.jxpcxt.service.NewUserService;
import com.inspur.jxpcxt.utils.NetworkUtils;
import com.inspur.jypcxt.R;

/**
 * 用户注册页面
 * 
 * @author xiangdeshan
 * 
 */
public class NewUserActivity extends FragmentActivity {
	// 学号
	private EditText mSnoEt = null;
	// 姓名
	private EditText mSnameEt = null;
	// 班号
	private EditText mCnoEt = null;
	// 密码
	private EditText mPswdEt = null;
	// 确认密码
	private EditText mAgainPswdEt = null;
	// 找回密码的问题
	EditText mFindPswdEt = null;
	// 提交
	private Button mRegisterBtn = null;
	// 返回按钮
	private ImageView mReturnBtn = null;
	// 图片
	private ImageView mBackgIv = null;

	// 服务器返回的结果
	String result = null;

	// 刷新UI
	Handler mHandler = null;

	@SuppressLint("HandlerLeak")
	@Override
	protected void onCreate(@Nullable Bundle arg0)
		{
			// TODO Auto-generated method stub
			super.onCreate(arg0);
			//requestWindowFeature(Window.FEATURE_NO_TITLE);
			setContentView(R.layout.activity_new_user);
			initUI();
			mHandler = new Handler() {
				@Override
				public void handleMessage(Message msg)
					{
						if (msg.what == Constants.REFRESH_UI) {
							refresh();
						}
					}
			};
		}

	/**
	 * 初始化UI
	 */
	private void initUI()
		{
			// 与UI关联
			mSnoEt = (EditText) findViewById(R.id.et_newuser_sno);
			mSnameEt = (EditText) findViewById(R.id.et_newuser_sname);
			mCnoEt = (EditText) findViewById(R.id.et_newuser_cno);
			mPswdEt = (EditText) findViewById(R.id.et_newuser_spswd);
			mAgainPswdEt = (EditText) findViewById(R.id.et_newuser_again_spswd);
			mFindPswdEt = (EditText) findViewById(R.id.et_find_spswd_question);
			mRegisterBtn = (Button) findViewById(R.id.btn_newuser_register);
			mBackgIv = (ImageView) findViewById(R.id.iv_backg);
			mReturnBtn = (ImageView) findViewById(R.id.iv_return);

			Random random = new Random();
			int i = Math.abs(random.nextInt()) % 5 + 1;
			try {
				Field field = R.drawable.class.getField("main_back" + i);
				int j = field.getInt(new R.drawable());
				mBackgIv.setBackgroundResource(j);
			} catch (Exception e) {
				mBackgIv.setBackgroundResource(R.drawable.main_back1);
			}

			// 点击返回按钮响应事件
			mReturnBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v)
					{
						// finish当前页面
						NewUserActivity.this.finish();
					}
			});
			// 点击提交时的响应事件
			mRegisterBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v)
					{
						// 获得输入的学号
						final String sno = mSnoEt.getText().toString().trim();
						// 获得输入的姓名
						final String sname = mSnameEt.getText().toString()
								.trim();
						// 获得输入的班号
						final String cno = mCnoEt.getText().toString().trim();
						// 获得输入的密码
						final String pswd = mPswdEt.getText().toString().trim();
						// 获得输入的确认密码
						String againPswd = mAgainPswdEt.getText().toString()
								.trim();
						// 获得输入找回密码的问题
						final String findPswd = mFindPswdEt.getText()
								.toString().trim();
						// 判断
						if (TextUtils.isEmpty(sno) || TextUtils.isEmpty(sname)
								|| TextUtils.isEmpty(cno)
								|| TextUtils.isEmpty(findPswd)) {
							Toast.makeText(NewUserActivity.this, "各项都不可为空",
									Toast.LENGTH_LONG).show();
							return;
						}
						if (!NetworkUtils
								.isConnectInternet(NewUserActivity.this)) {
							Toast.makeText(NewUserActivity.this,
									"网络不可用，请检查网络连接", Toast.LENGTH_SHORT).show();
							return;
						}

						// 判断两次密码输入相同才会请求服务器
						if (!TextUtils.isEmpty(pswd) && pswd.equals(againPswd)) {

							// 利用异步栈访问网路
							new AsyncTask<Void, Void, String>() {

								@Override
								protected String doInBackground(Void... params)
									{
										// TODO Auto-generated method stub
										result = NewUserService
												.NewUserByClientGet(sno, sname,
														cno, pswd, findPswd);
										return result;
									}

								protected void onPostExecute(String result)
									{
										if (result == null) {
											return;
										}
										if (result.equals("注册成功")) {

											Toast.makeText(
													NewUserActivity.this,
													result, Toast.LENGTH_LONG)
													.show();
											Intent intent = new Intent(
													NewUserActivity.this,
													LoginActivity.class);
											startActivity(intent);
											NewUserActivity.this.finish();
										} else {
											Toast.makeText(
													NewUserActivity.this,
													result, Toast.LENGTH_LONG)
													.show();
											mHandler.sendEmptyMessage(Constants.REFRESH_UI);
										}
									};

							}.execute();
						} else {
							Toast.makeText(NewUserActivity.this, "再次核实密码！！！",
									Toast.LENGTH_LONG).show();
						}
					}
			});

		}

	/**
	 * 刷新页面
	 */
	public void refresh()
		{
			NewUserActivity.this.finish();
			Intent intent = new Intent(NewUserActivity.this,
					NewUserActivity.class);
			startActivity(intent);
		}
}
